package libs;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Utils {

	public static List<Class<?>> getAllClasses(String pckgname) {
		try {
			List<Class<?>> classes = new ArrayList<Class<?>>();
			// Get a File object for the package
			File directory = null;
			try {
				directory = new File(Thread.currentThread().getContextClassLoader()
						.getResource(pckgname.replace('.', '/')).getFile());
			} catch (NullPointerException x) {
				System.out.println("Nullpointer");
				throw new ClassNotFoundException(pckgname + " does not appear to be a valid package");
			}
			if (directory.exists()) {
				// Get the list of the files contained in the package
				String[] files = directory.list();
				for (int i = 0; i < files.length; i++) {
					// we are only interested in .class files
					if (files[i].endsWith(".class")) {
						// removes the .class extension
						classes.add(Class.forName(pckgname + '.' + files[i].substring(0, files[i].length() - 6)));
					}
				}
			} else {
				System.out.println("Directory does not exist");
				throw new ClassNotFoundException(pckgname + " does not appear to be a valid package");
			}
			return classes;

		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<Class<?>>();
		}
	}
	
	public static String getDatetimeString(long datetime){
		long subTime = (new Date()).getTime() - datetime;

		long oYear = (long) 365 * 24 * 60 * 60 * 1000;
		long oMonth = (long) 30 * 24 * 60 * 60 * 1000;
		long oDay = (long) 24 * 60 * 60 * 1000;
		long oHour = 60 * 60 * 1000;
		long oMin = 60 * 1000;
		
		// years
		if(subTime > oYear){
			return (int) Math.ceil(subTime/oYear) + " years ago";
			
		// months
		}else if(subTime > oMonth){
			return (int) Math.ceil(subTime/oMonth) + " months ago";
			
		// days
		}else if(subTime > oDay){
			return (int) Math.ceil(subTime/oDay) + " days ago";
			
		// hours
		}else if(subTime > oHour){
			return (int) Math.ceil(subTime/oHour) + " hours ago";
			
		// mins
		}else if(subTime > oMin){
			return (int) Math.ceil(subTime/oMin) + " mins ago";
		}else{
			return "a few seconds ago";
		}
	}
}
